<?php 


class Home{ 
	
	
	public static $msgs = array();
		
	function __construct()
	{
		//$this->user_id = $_SESSION['user_id'];		
	}

	function getTickerNews()
	{
		global $db;
		$sql = "SELECT * FROM news where active = 1 AND show_in_ticker = 1 order by publish_date desc, news_id desc limit 0,8";
		$result = $db->fetch_all($sql);
		return $result;	
	}
	
	function getFeaturedNews()
	{
		global $db;
		$sql = "SELECT * FROM news where active = 1 AND topstory = 1 order by publish_date desc, news_id desc limit 0,3";
		$result = $db->fetch_all($sql);
		return $result;	
	}
	
	
	function getPressReleases($data = array())
	{
		global $db;
		$sql = "SELECT *, date_format(publish_date, '%b %d,%Y') as publish_date FROM news where active = 1 AND (news_type = 'press_release' OR news_type = 'both') order by news.publish_date desc, news_id desc limit 0, 7";
		$result = $db->fetch_all($sql);
		
		return $result;	
	}
	function getRightBanner()
	{ 
	         global $db;
			 $sql = "SELECT b . *,bs.*
					 FROM banners AS b
					 INNER JOIN banner_size AS bs
					 ON b.banner_size_id = bs.size_id
					 WHERE banner_position = 'right'
					 ORDER BY banner_order
					 LIMIT 0 , 2 ";
			 $result = $db->fetch_all($sql);
			 foreach($result as &$row)
			 {
   				$parts = explode("x", $row['banner_size']);
   				$row['witdh'] = $parts[0];
   				$row['height'] = $parts[1];
			 }
			 //print_r($row['witdh'].$row['height']);exit;
		     return $result;
	}
	function getTopCenterBanner()
	{ 
	         global $db;
			 $sql = "SELECT b . *,bs.*
					 FROM banners AS b
					 INNER JOIN banner_size AS bs
					 ON b.banner_size_id = bs.size_id
					 WHERE banner_position = 'Top Center'
					 LIMIT 0 , 4";
			 $result = $db->fetch_all($sql);
			 foreach($result as &$row)
			 {
   				$parts = explode("x", $row['banner_size']);
   				$row['witdh'] = $parts[0];
   				$row['height'] = $parts[1];
			 }
			 //print_r($row['witdh'].$row['height']);exit;
		     return $result;
	}
	function getBottomBanner()
	{ 
	         global $db;
			 $sql = "SELECT b . *,bs.*
					 FROM banners AS b
					 INNER JOIN banner_size AS bs
					 ON b.banner_size_id = bs.size_id
					 WHERE banner_position = 'Bottom'
					 LIMIT 0 , 1";
			 $result = $db->fetch_all($sql);
			 foreach($result as &$row)
			 {
   				$parts = explode("x", $row['banner_size']);
   				$row['witdh'] = $parts[0];
   				$row['height'] = $parts[1];
			 }
			 //print_r($row['witdh'].$row['height']);exit;
		     return $result;
	}
	function getCenterBottomBanner()
	{ 
	         global $db;
			 $sql = "SELECT b . *,bs.*
					 FROM banners AS b
					 INNER JOIN banner_size AS bs
					 ON b.banner_size_id = bs.size_id
					 WHERE banner_position = 'Center Bottom'
					 LIMIT 0 , 1";
			 $result = $db->fetch_all($sql);
			 foreach($result as &$row)
			 {
   				$parts = explode("x", $row['banner_size']);
   				$row['witdh'] = $parts[0];
   				$row['height'] = $parts[1];
			 }
			 //print_r($row['witdh'].$row['height']);exit;
		     return $result;
	}
	
	//==================Start code Edit By Sajjad=============================
	/*
   *** this function is used for show featued photo on home page
   */
	function getFeaturedPhotoGallery()
	{
		global $db;
		$sql = "SELECT photo_title,photo_description ,file_name, large_file , thumb_file FROM photo where photo_status = 1 AND featured = 1 ORDER BY photo.photo_order ASC , photo_id DESC limit 0, 10";
		$result = $db->fetch_all($sql);
		return $result;
	}	
   //==================End code Edit By Sajjad=============================	
	
	/*************Edit Start By M SHAHZAD***********************/

   function getFixturesMatches__(){
	    global $db;
		
 		$sql = "SELECT tournament_id , tournament_type_id , hosted_country_id
				FROM pcb_tournament t group by tournament_id";
		$results = $db->fetch_all($sql);
 		foreach($results as &$rows){
			
				$tournament_id 		= $rows['tournament_id'];
				$tournament_type_id = $rows['tournament_type_id'];
				$hosted_country_id	= $rows['hosted_country_id'];
 					 			
					$sql_ = "SELECT ts.tournament_schedule_id, ts.match_name, date_format(ts.match_start_date, '%d %M %Y') as start_date, 
							date_format(ts.match_start_date, '%H:%i:%s') as start_time ,
							getGroundNameById(ground_id) as ground_name, getCityNameById(city_id) as city_name
							
							FROM pcb_tournament_schedule ts
							
							WHERE ts.tournament_schedule_id not in (select tournament_schedule_id from pcb_match) 
							and ts.tournament_id= '".$tournament_id."'
							order by start_date,start_time asc" ; 
							
					$result_ = $db->fetch_all($sql_);
					foreach($result_ as &$row){
						 $rows[$tournament_id] = $row ;
 					}
  	}//foreach loop	

}// getFixturesMatches

   function getFixturesMatches(){
	   global $db;
		
	   	$sql = "SELECT 
						ts.tournament_schedule_id, ts.match_name, match_start_date, 
						date_format(ts.match_start_date, '%H:%i') as start_time ,
						date_format(ts.match_start_date, '%a %b %e') as start_date_formated, 
						date_format(ts.match_end_date, '%a %b %e') as end_date_formated, 
						getGroundNameById(ground_id) as ground_name, getCityNameById(city_id) as city_name
				FROM 
					pcb_tournament_schedule ts
				WHERE 
					ts.tournament_schedule_id not in (select tournament_schedule_id from pcb_match)
					order by match_start_date" ; 
				
		$result = $db->fetch_all($sql);
		foreach($result as &$row){
			
 			$row['start_time_gmt'] = $this->addTime($row['match_start_date'], 5);
			$row['match_start_date'] = $row['start_date_formated'];
			$row['match_end_date'] = $row['end_date_formated'];
			
			// teams data
			$teams = $this->getMatchTeams($row['tournament_schedule_id']);
			$row['team_1_name'] = $teams[0]['team_name'];
			$row['team_2_name'] = $teams[1]['team_name'];
		}
 		return $result ;
	   
   }// getFixturesMatches
	
	
	function addTime($date_time, $offset = 0)
	{
		if($date_time == '')
		{
			return '';
		}
		list($date,$time) =  split(" " ,$date_time);
		list($year,$month,$day)  =	  split("-" ,$date);
		list($hr,$min,$sec)  =	  split(":" ,$time);
				
		$timestamp = mktime($hr+$offset,$min,$sec,$month,$day,$year);
		$time = date("H:i", $timestamp); 
		return $time;
	}
	
	
	function matchDateFormat($match_start_date){
			
			list($date,$time) =  split(" " ,$match_start_date);
			list($year,$month,$day)  =	  split("-" ,$date);
			list($hr,$min,$sec)  =	  split(":" ,$time);
			
			$date_time = mktime($hr,$min,$sec,$month,$day,$year);
			
			
	}//matchDateFormat
	function getMatchTeams($tournament_schedule_id){
		
		global $db;
		$sql = "select tt.tournament_team_id, t.team_name  ,tt.tournament_team_id
				FROM pcb_tournament_team tt, pcb_teams t, pcb_match_teams mt
				WHERE 
				tt.team_id = t.team_id and 
				tt.tournament_team_id = mt.tournament_team_id and 
				mt.tournament_schedule_id = ".intval($tournament_schedule_id);
		return $db->fetch_all($sql);
	}

/*************Edit END By M SHAHZAD***********************/

/************  Code added by mohsin for match results ***************/

	function getMatchResults()
	{
		global $db, $config;	
		$sql = "SELECT 
					m.match_id,  
					ts.match_name, match_start_date,
					date_format(ts.match_start_date, '%e') as start_day, 
					date_format(ts.match_end_date, '%e') as end_day, 
					date_format(ts.match_start_date, '%Y') as match_year, 
					date_format(ts.match_start_date, '%b') as match_month, 
					getGroundNameById(ts.ground_id) as ground_name, getCityNameById(ts.city_id) as city_name,
					m.match_status ,m.current_match_type, m.match_summary, pcb_country.country_name
				FROM 
					pcb_tournament_schedule ts, 
					pcb_match m,
					pcb_country
					
				WHERE 
					ts.tournament_schedule_id 	= m.tournament_schedule_id  
					AND ts.country_id = pcb_country.country_id
					AND m.match_result != 'pending'
					order by ts.match_start_date desc" ;
		
		$results = $db->fetch_all($sql);		
		
		foreach($results as &$rows)
		{  
			$match_id = intval($rows['match_id']);
			$team_info= $this->getMatchTeamByMatchId($match_id);
			
			$match_days = $rows['match_month'].' '.$rows['start_day'];
			if( $rows['start_day'] != $rows['end_day'] )
			{
				$match_days .= '-'.$rows['end_day'];
			}
			
			$rows['match_days'] = $match_days.', '.$rows['match_year'];
			$rows['match_scores'] = $this->getMatchInningScores($rows);
			
			$team_name_1 = $team_info[0]['team_name'];
			$team_name_2 = $team_info[1]['team_name'];
			
			/*$tournament_team_id_1 = $team_info[0]['tournament_team_id'];
			$tournament_team_id_2 = $team_info[1]['tournament_team_id'];
			$team_logo_1 = $config['baseurl']."/images/teams/".$team_info[0]['team_logo'];
			$team_logo_2 = $config['baseurl']."/images/teams/".$team_info[1]['team_logo'];*/
			
			$rows['team_1'] = $team_name_1;
			$rows['team_2'] = $team_name_2;
			
		}
		
		return $results;
	}
	
	
	function getMatchTeamByMatchId($match_id)
	{
		global $db, $config;
		$sql = "select pcb_tournament_team.tournament_team_id , pcb_teams.team_name ,match_id ,team_logo
				from pcb_match_teams  ,  pcb_tournament_team , pcb_teams
				where pcb_match_teams.tournament_team_id = pcb_tournament_team.tournament_team_id
				and pcb_teams.team_id = pcb_tournament_team.team_id and match_id = '".$match_id."'" ; 
 		$data = $db->fetch_all($sql);	
		return $data ; 	
	}
	
	function getMatchInningScores($data)
	{
		global $db;
		$sql = "SELECT *, getTeamNameByTournamentTeamIdl(batting_team_id) as bating_team_name FROM pcb_match_inning WHERE match_id = '".$data['match_id']."' ORDER BY match_inning_id ASC";
		$innings_data =  $db->fetch_all($sql);
		
		$team_1_str = '';
		$team_2_str = '';
		
		$array_team_1 = array();
		$array_team_2 = array();
		
		$first_batting_team_id = $innings_data[0]['batting_team_id'];
		
		foreach($innings_data as $row)
		{
			if( $row['batting_team_id'] == $first_batting_team_id )
			{
				$array_team_1[] = $row;
			}
			else
			{
				$array_team_2[] = $row;
			}
		}
		
	  $team_1_innings = count($array_team_1);
	  $team_2_innings = count($array_team_2);
	
		if	( $team_1_innings > 0 )
		{
			
			$team_1_str = $array_team_1[0]['bating_team_name'].' ';
			if ( $array_team_1[0]['total_wickets'] == 10 )
			{
				$team_1_str .= $array_team_1[0]['total_runs'];
			}
			else
			{
				$team_1_str .= $array_team_1[0]['total_runs']."/".$array_team_1[0]['total_wickets'];
			}
			
			if	( $team_1_innings > 1 ) 
			{
				$team_1_str .= ' and ';
				if ( $array_team_1[1]['total_wickets'] == 10 )
				{
					$team_1_str .= $array_team_1[1]['total_runs'];
				}
				else
				{
					$team_1_str .= $array_team_1[1]['total_runs']."/".$array_team_1[1]['total_wickets'];
				}
				
			}

			$team_1_str .= "; ";	
		}
		
		
		if	( $team_2_innings > 0 )
		{
			$team_2_str = $array_team_2[0]['bating_team_name'].' ';
			if ( $array_team_2[0]['total_wickets'] == 10 )
			{
				$team_2_str .= $array_team_2[0]['total_runs'];
			}
			else
			{
				$team_2_str .= $array_team_2[0]['total_runs']."/".$array_team_2[0]['total_wickets'];
			}
			if	( $team_2_innings > 1 ) 
			{
				$team_2_str .= ' and ';
				if ( $array_team_2[1]['total_wickets'] == 10 )
				{
					$team_2_str .= $array_team_2[1]['total_runs'];
				}
				else
				{
					$team_2_str .= $array_team_2[1]['total_runs']."/".$array_team_2[1]['total_wickets'];
				}
			}

			$team_2_str .= "; ";	
		}
	  	  
		$innings_result = $team_1_str . $team_2_str;
	
		return $innings_result;
	}

/************  END Code added by mohsin for match results ***************/

	
}
?>